package qnb._config;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class StringToDate implements Converter<String, Date> {


	@Override
	public Date convert(String source) {
		System.out.println("来自前端页面表单的值（String转Date类型触发）：" + source);
		if (null == source) {
			return null;
		}
		SimpleDateFormat sf=null;
		if(source.matches("^([0-9]{4}-[0-9]{2}-[0-9]{2})$")) {
			 sf= new SimpleDateFormat("yyyy-MM-dd");
		}else if(source.matches("^([0-9]{4}/[0-9]{2}/[0-9]{2})$")) {
			sf= new SimpleDateFormat("yyyy/MM/dd");
		}else {
			return null;
		}
		try {
			return sf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
